If you're looking for a job in the world of Java development, then you'll need to be prepared for some tough spring boot interview questions. Spring Boot is a powerful Java framework that makes it easy to create stand-alone, production-grade applications. In this blog post, we will discuss some of the most common spring boot interview questions and Answers and provide answers to help you prepare for your next interview!
Ans: Spring applications require developers to perform a lot of configuration. For example, developers need to know and specify all the dependencies, configure the data sources to connect to the database, do the front-end configuration like setting up the view resolvers (for a web application), etc. When Spring Boot is used, developers do not need to do any of this. Developers just need to configure the type of application being developed and Spring Boot automatically configures the application using suitable default values.
Ans: Spring Boot can be installed like any other Java library, you just need to include the appropriate JAR files on the class path. Spring Boot provides Maven as well as Gradle dependencies. In addition, Spring Boot also supports a command-line interface (CLI). This is available as a compressed zip/tar.gz file and can be installed simply by uncompressing these files.
Ans: Spring Boot CLI is a command line interface that can be used to get started with Spring Boot easily. It lets you run Groovy scripts which help to get started with Spring Boot without requiring all the boilerplate code. In order to use it, you need to do the following:
a) Download and install the Spring Boot CLI
b) Create a Groovy script
c) Run the groovy script using spring run <groovy_script_name>
Ans: Spring Boot auto-configuration simply means that Spring Boot configures the application based on the dependencies on the classpath. Autoconfiguration can be enabled by specifying either the @EnableAutoConfiguration annotation or the @SpringBootApplication annotation. This then configures the application automatically by scanning the classpath and registering beans based on the jar files availableon the classpath. So, for example, if Spring Boot finds MySQL database driver on the classpath, it automatically configures a MySQL data source.
Do you want to enrich your career by learning Spring Boot? Then Enroll in "Spring Boot Training", this course will help you to boost your career. |
Ans: In order to disable autoconfiguration, you need to specify the exclude attribute either on the @SpringBootApplication annotation or the @EnableAutoConfiguration annotation as shown below:
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
public class MyApplication {
}
OR
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyApplication {
}
This code disables DataSourceAutoConfiguration. So, a data source will not be automatically configured based on the dependencies on the classpath.
Autoconfiguration can also be disabled via the spring.autoconfigure.exclude property.
Ans: Spring Boot starters are nothing but some dependency descriptors that can be included in the POM file. They help to get started with Spring Boot easily. When a starter is specified, the starter in turn adds all the necessary jar files required for the particular starter.
Some important Spring Boot starters are as follows:
Ans: The spring-boot-starter-data-jpa is a JPA starter and can be used to get started with Spring Data JPA with Hibernate in an application. It can be added using the following Maven dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
This adds the dependencies for Spring Data JPA, Hibernate, and Spring ORM.
Ans: In order to develop a REST application using Spring Boot, you need to do the following:
a) Include the spring-boot-starter-web. This automatically includes an embedded Tomcat container.
b) Create a Controller class that has the @RestController annotation and provide code for the end points that you’d like to handle
c) Create a class that has the @SpringBootApplication annotation and a main method that starts the application
Ans: The @SpringBootApplication annotation is specified on the main class of an application. It tells Spring Boot to scan for Spring components within the current package. It also tells Spring to enable auto-configuration (creating beans automatically based on the classpath). So, it is equivalent to specifying the @EnableAutoConfiguration, @Configuration, and the @ComponentScan annotations.
Ans: Spring Initializer is a web-based service that helps to get started with a Spring application easily. In order to create a Spring Boot application using Spring Initializer, you need to open up https://start.spring.io. Here, you need to select values for the type of project being created, type of dependencies required, type of build system being used (Maven or Gradle), etc. It then generates a project structure for you based on your build system. So, if you choose Maven, it generates a Maven project for you which can then be easily imported in your IDE.
Ans: While creating a Spring Boot application, you need to create a class with the main method as follows:
@SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class, args);
}
}
The class needs to be designated with the @SpringBootApplication annotation. Within the main method, you need to invoke the SpringApplication.run method as shown above. This allows running a Spring Boot application as a standard Java application.
Ans: In order to develop a web application, Spring Boot provides a spring-boot-starter-web dependency. This includes the embedded Tomcat server by default. However, if you’d like to use Jetty instead of Tomcat, you need to specify the following in the Maven POM file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
This causes Tomcat to be excluded and Jetty to be included.
Ans: All the configuration information required by Spring Boot needs to be specified within the application.properties file. This can include properties related to database configuration, logging configuration, security related properties, server related properties, etc. The following are some of the properties that can be specified within the application.properties file:
a) logging.config – Path of the logging configuration file
b) spring.datasource.url – Specifies the database URL
c) server.port – Specifies the HTTP port
Ans: In order to configure SSL support in a Spring Boot application, the following needs to be done:
a) Obtain an SSL certificate (A self-signed certificate can be obtained via keytool)
b) Specify the following SSL related properties:
server.port=8443
server.ssl.key-store=<key store file path>
server.ssl.key-store-password=< key store password>
server.ssl.key-store-type=<key store type>
server.ssl.key-alias=<key store alias>
c) Restart the application. This causes Tomcat to start on port 8443.
Ans: By default, a Spring BOOT REST service produces a JSON output. In order to produce an XML output, you can do one on the following:
a) Add the following jackson-dataformat-xml dependency as follows:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
b) If you are using Java 8, you do not need to add any additional dependency. You just need to add the XmlRootElement annotation to your class
c) If you are using a Java version other than Java 8, you can also add the jaxb dependency as follows:
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
However, this will also require you to annotate your class with the @XmlRootElement annotation.
Ans: Spring Boot supports Tomcat, Jetty, and Undertow embedded containers for servlet applications. Tomcat is the default container that is included when the spring-boot-starter-web dependency is used. Spring Boot also supports the Reactor Netty, Jetty, Undertow, and Tomcat for webflux application. In this case Reactor Netty is the default and is included with the spring-boot-starter-webflux dependency.
Ans: In order to add ThymeLeaf support to a Spring Boot application, you need to do the following:
a) Add the following dependency to the POM file:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
b) Create HTML files corresponding to the Thymeleaf templates and place them in the templates folder of your application
c) Redirect to the Thymeleaf templates from within your controller
Ans: You can run a Spring Boot application in one of the following ways:
a) Directly from an IDE – You can run a Spring Boot application directly from an IDE as a Java application
b) As a Jar – You can create an executable jar and then run your Spring Boot application using the following command:
java -jar <jar-file-path>
c) Via the Spring Boot Maven plugin – The Spring Boot maven plugin can be used to run a Spring Boot application as follows:
mvn spring-boot:run
d) Via the Spring Boot Gradle plugin – The Spring Boot Gradle plugin can be used to run a Spring Boot application as follows:
e) gradle bootRun
Ans: Properties can be specified at the command line as a command line option using – followed by the property name. For example, suppose you want to enable http2 via the command line, you can specify the server.http2.enabled property at the command line as follows:
java -jar <jar-file-path> --server.http2.enabled=true
Ans: By default, Spring Boot uses Logback as the logging library. Logback is automatically included when you add the Spring Boot starters (For example, the spring-boot-starter-web includes the spring-boot-starter-logging which includes support for Logback). Also, Spring Boot configures logback with default values so developers do not have to do any configuration. By default, the logging level is set to INFO and the logs are printed on the console. However, this can be changed easily by specifying the appropriate properties.
Ans: In order to configure Spring Boot with MongoDB, you need to include spring-boot-starter-data-mongodb starter. You also need to write code as follows:
import org.springframework.data.mongodb.MongoDatabaseFactory;
import com.mongodb.client.MongoDatabase;
@Component
public class MongoDBBean {
private final MongoDatabaseFactory mongoDBFactory;
@Autowired
public MongoDBBean(MongoDatabaseFactory mongoDBFactory) {
this.mongoDBFactory = mongoDBFactory;
}
public void connectToDB() {
MongoDatabase database = mongoDBFactory.getMongoDatabase();
// other database code
}
}
By default, this code tries to connect to a MongoDB server at
mongodb://localhost/test
Ans: The spring-boot-starter-test is a Spring Boot starter that can be used to enable testing. It provides the following libraries:
Ans: Spring Boot supports H2, Apache Derby, and HSQL in-memory databases. You just need to specify the dependency of the database that you’d like to include and Spring Boot then auto-configures the database using suitable defaults. For example, if you’d like to use H2, you need to specify the following in the Maven POM file:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
This automatically configures an in-memory H2 database that connects to a database called testdb with username as sa and password as empty. If you’d like to customize the configuration, you can specify different values as specified below:
spring.datasource.url=jdbc:h2:mem:<db name>
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=<username>
springspring.datasource.password=<password>
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Ans: Spring Boot supports Jackson, JSON-B, and Gson JSON libraries. Spring Boot automatically configures the JSON library based on the dependencies on the classpath. So, for example, if Gson is present on the classpath, it automatically configures Gson. Jackson is the default JSON library. It is part of the spring-boot-starter-json.
Ans: Spring Boot supports JAX-RS with Jersey. In order to use this, you need to add the spring-boot-starter-jersey starter. You need to create a class that registers all the end points as follows:
@Component
public class MyConfig extends ResourceConfig {
public MyConfig () {
register(MyService.class);
}
}
Finally, you need to create a class corresponding to the endpoint as follows:
@Component
@Path("/sayhello")
public class MyService {
@GET
public String sayHello() {
return "Hello";
}
}
Ans: Spring Boot Dev Tools includes some tools that make the development process easier. It can be enabled by adding the spring-boot-devtools starter. Once added, it supports features like automatic application restart (when there is a change in the classpath), live reload (automatically triggering a browser refresh when there is a resource change), Disabling cache, Enabling debug level logging, etc. All these features are very useful in a development environment.
Ans: The @SpringBootTest application can be used for integration testing. It creates and initializes an ApplicationContext (which is required for a Spring application) and automatically starts the embedded container. It needs to be specified in a test class. Dependencies can also be injected into the test classes via the @Autowired annotation.
Ans: Spring Boot provides a class called BasicErrorController. In order to display a custom error page, you simply need to create an HTML file corresponding to the error page and place it in the resources/templates directory of your application. This then gets picked up BasicErrorController. If you’d like to further customize the error handling then you need to create a class that implements the ErrorController interface.
Ans: In order to create an executable JAR of a Spring Boot application, the following needs to be added to the Maven POM file:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
You can then create an executable jar file using the following maven command:
mvn package
This creates an executable jar file in the target directory of your computer.
Ans: In order to secure a Spring Boot application, the spring-boot-starter-security needs to be included as follows:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
You also need to create a class that extends WebSecurityConfigurerAdapter and provides an appropriate implementation for the configure method.
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.